package com.bnz.service.impl;


import com.bnz.dao.StudentDao;

import com.bnz.entity.Student;

import com.bnz.service.IStudentService;

import com.bnz.vo.PageVo;
import com.bnz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Arrays;


/**
 * @author xy
 * @date 2022/10/21
 */
@Service
public class StudentServiceImpl    implements IStudentService {

    @Autowired
     StudentDao studentDao;



//    @Override
//    public PageVo<Student> search(StudentVo vo) {
//        Page<Student> pageStu = this.query().like(!StringUtils.isEmpty(vo.getSname()), "sname", vo.getSname())
//                .like(!StringUtils.isEmpty(vo.getAddr()), "addr", vo.getAddr())
//                .eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid())
//                .page(new Page<>(vo.getPage(),vo.getPageSize()));
//        for (Student st : pageStu.getRecords()) {
//            Classes classes =classesService .getById(st.getCid());;
//            st.setCname(classes.getCname());
//        }
//        return new  PageVo( pageStu.getTotal(),pageStu.getRecords());
//    }


    @Override
    public PageVo<Student> search(StudentVo vo) {
        Page<Student> pageStu = studentDao.findDemo(vo.getSname(),vo.getAddr(),vo.getCid(),PageRequest.of(vo.getPage()-1,vo.getPageSize()));
        return new  PageVo( pageStu.getTotalElements(),pageStu.getContent());
    }

    @Override
    public void add(Student student) {
        studentDao.save(student);
    }

    @Override
    public int update(Student student) {
        studentDao.save(student);
        return 0;
    }

    @Override
    public void delete(Integer[] sid) {

        for (Integer i : sid) {
            studentDao.deleteById(i);
        }
    }

    @Override
    public void deleteById(Integer id) {
        studentDao.deleteById(id);
    }
//
//    @Override
//    public void delete(String[] sid) {
//        this.removeByIds(Arrays.asList(sid));
//    }
}
